var t = getApp(), a = void 0;

Page({
    data: {
        realname: "",
        id_number: "",
        card_z_img: "",
        card_f_img: "",
        is_watch: !1,
        formId: "",
        city: [],
        cityFlag: -1,
        url: t.com.API,
        open_agent: 0
    },
    realnameInput: function(t) {
        this.setData({
            realname: t.detail.value
        });
    },
    numberInput: function(t) {
        this.setData({
            id_number: t.detail.value
        });
    },
    cityChange: function(t) {
        this.setData({
            cityFlag: t.detail.value
        });
    },
    getAgentCity: function() {
        wx.showLoading({
            title: "加载中",
            mask: !0
        }), t.com.post("wx/agent/city", {}, function(t) {
            wx.hideLoading(), 0 === t.errno && a.setData({
                city: t.data.data,
                open_agent: t.data.open_agent
            });
        });
    },
    onLoad: function(t) {
        if (a = this, t.data) {
            var e = JSON.parse(t.data);
            this.setData({
                realname: e.realname,
                id_number: e.id_number,
                card_z_img: e.card_z_img,
                card_f_img: e.card_f_img,
                id: e.id
            });
        }
        this.getAgentCity();
    },
    uploadImg: function(e) {
        e.detail.formId && this.setData({
            formId: e.detail.formId
        });
        var i = e.currentTarget.dataset.type;
        wx.chooseImage({
            count: 1,
            success: function(e) {
                wx.showLoading({
                    title: "加载中",
                    mask: !0
                }), wx.uploadFile({
                    url: t.com.API + "wx/file/uploadCard",
                    filePath: e.tempFilePaths[0],
                    name: "image",
                    header: {
                        "content-type": "application/x-www-form-urlencoded",
                        token: wx.getStorageSync("token")
                    },
                    success: function(t) {
                        wx.hideLoading(), 0 === (t = JSON.parse(t.data)).errno && (1 == i ? a.setData({
                            card_z_img: t.data.url
                        }) : a.setData({
                            card_f_img: t.data.url
                        }));
                    }
                });
            }
        });
    },
    checkboxChange: function(t) {
        this.setData({
            is_watch: 1 == t.detail.value.length
        });
    },
    nextform: function(t) {
        this.setData({
            formId: t.detail.formId
        });
    },
    submit: function() {
        if ("" == this.data.realname) wx.showToast({
            title: "请填写真实姓名",
            icon: "none"
        }); else if (18 != this.data.id_number.length) wx.showToast({
            title: "请填写18位身份证号码",
            icon: "none"
        }); else if (1 == this.data.open_agent && -1 == this.data.cityFlag) wx.showToast({
            title: "请选择一个城市",
            icon: "none"
        }); else if ("" == this.data.card_z_img) wx.showToast({
            title: "请上传身份证正面",
            icon: "none"
        }); else if ("" == this.data.card_z_img) wx.showToast({
            title: "请上传身份证反面",
            icon: "none"
        }); else {
            wx.showLoading({
                title: "提交中",
                mask: !0
            });
            var a = "register";
            this.data.id && (a = "update"), t.com.post("wx/service/" + a, {
                realname: this.data.realname,
                id_number: this.data.id_number,
                card_z_img: this.data.card_z_img,
                card_f_img: this.data.card_f_img,
                formId: this.data.formId,
                city: 1 == this.data.open_agent ? this.data.city[this.data.cityFlag].city_name : ""
            }, function(t) {
                wx.hideLoading(), 0 === t.errno && (wx.showToast({
                    title: t.errmsg,
                    duration: 1e3
                }), setTimeout(function() {
                    wx.redirectTo({
                        url: "/pages/runman/verify/verify"
                    });
                }, 1e3));
            });
        }
    }
});